package cn.gov.pbc.gd.credit.user.service.login.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.liu.jbase.BaseVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.annotation.LogAnnotation;
import com.liu.jbase.constant.ResCode;
import com.liu.jbase.service.impl.BaseServiceImpl;
import com.liu.tools.DateUtil;
import com.liu.tools.FileUtil;
import com.liu.tools.IPUtil;
import com.liu.tools.LogUtil;
import com.liu.tools.NetHelper;
import com.liu.tools.RandomUtil;
import com.liu.tools.StrUtil;

import cn.gov.pbc.gd.credit.api.rolemenu.IRoleMenuApi;
import cn.gov.pbc.gd.credit.api.user.IUserApi;
import cn.gov.pbc.gd.credit.api.work.IWorkApi;
import cn.gov.pbc.gd.credit.api.work.add.IAddApi;
import cn.gov.pbc.gd.credit.center.service.user.ICCUserService;
import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.constant.CommonConstant;
import cn.gov.pbc.gd.credit.common.constant.LogConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.service.redis.IRedisService;
import cn.gov.pbc.gd.credit.common.util.UserUtil;
import cn.gov.pbc.gd.credit.common.vo.LoginVo;
import cn.gov.pbc.gd.credit.dfs.service.file.IFileService;
import cn.gov.pbc.gd.credit.user.service.dictionary.IDictionaryService;
import cn.gov.pbc.gd.credit.user.service.login.ILoginService;
import cn.gov.pbc.gd.credit.user.service.menu.IMenuService;
import cn.gov.pbc.gd.credit.user.service.online.IOnlineUserService;
import cn.gov.pbc.gd.credit.user.service.realuser.IRealUserService;
import cn.gov.pbc.gd.credit.vo.center.ReqCCUserVo;
import cn.gov.pbc.gd.credit.vo.center.ResCCUserVo;
import cn.gov.pbc.gd.credit.vo.dictionary.DictionaryVo;
import cn.gov.pbc.gd.credit.vo.dictionary.ResDictionaryVo;
import cn.gov.pbc.gd.credit.vo.file.FileVo;
import cn.gov.pbc.gd.credit.vo.menu.MenuVo;
import cn.gov.pbc.gd.credit.vo.menu.ResMenuVo;
import cn.gov.pbc.gd.credit.vo.online.OnlineUserVo;
import cn.gov.pbc.gd.credit.vo.realuser.ResRealUserVo;
import cn.gov.pbc.gd.credit.vo.role.ResRoleVo;
import cn.gov.pbc.gd.credit.vo.rolemenu.RoleMenuVo;
import cn.gov.pbc.gd.credit.vo.user.ResUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;
import cn.gov.pbc.gd.credit.vo.work.ResWorkVo;
import cn.gov.pbc.gd.credit.vo.work.WorkVo;
import cn.gov.pbc.gd.credit.vo.work.add.AddVo;
import cn.gov.pbc.gd.credit.vo.work.add.ResAddVo;

@Service
public class LoginServiceImpl extends BaseServiceImpl<ResUserVo, UserVo, IUserApi> implements ILoginService{
	
	private static Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);
	
	@Autowired
	private IUserApi userApi;
	
	@Autowired
	private IRedisService redisService;	
	
	@Autowired
	private ICCUserService ccUserService;
	
	@Autowired
	private IRealUserService realUserService;

	@Autowired
	private IFileService fileService;
	
	@Autowired
	private IDictionaryService dictionaryService;
	
	@Autowired
	private IRoleMenuApi roleMenuApi;
	
	@Autowired
	private IMenuService menuService;
	
	@Autowired
	private IOnlineUserService onlineUserService;
	
	@Autowired
	private IWorkApi workApi;
	
	@Autowired
	private IAddApi addApi;
	
	private static Executor executor = Executors.newFixedThreadPool(5);
	
	@LogAnnotation(moduleName=LogConstant.MODULE_USER, option=LogConstant.LOGIN, logType = LogConstant.LOG_LOGIN, content="用户登录")
	@Override
	public ResultVo<UserModel> login(LoginVo vo, HttpServletRequest request) {
				
		ResultVo<UserModel> resultVo = loginValid(vo, request);
		
		if (! resultVo.getCode().equals(CodeConstant.SUCCESS)) {
			
			return resultVo;
		}
		
		UserVo userVo = new UserVo();
		userVo.setAccount(vo.getAccount());
		ResUserVo resUserVo = userApi.query(userVo);
		
		if(resUserVo == null){
			logger.error(LogUtil.msg("LoginServiceImpl:login", "用户账户不存在", JSON.toJSONString(vo)));
			resultVo.setCode(CodeConstant.DATA_NOT_EXSIT);
			resultVo.setMsg("用户不存在或密码不正确");
			return resultVo;
		}
		
		if (resUserVo.getStatus() != 1) {
			
			logger.error(LogUtil.msg("LoginServiceImpl:login", "用户账号未审批", JSON.toJSONString(vo)));
			resultVo.setCode(CodeConstant.DATA_NOT_EXSIT);
			resultVo.setMsg("用户账号审批中");
			return resultVo;
		}
		
		int authType = vo.getAuthType();
		
		switch (authType) {
		
		case CommonConstant.AUTH_TYPE_FACE:
			
			break;
		case CommonConstant.AUTH_TYPE_SMS:
			break;
		case CommonConstant.AUTH_TYPE_FINGER:			
			break;
		case CommonConstant.AUTH_TYPE_PWD:
			String pwd = UserUtil.getPwd(resUserVo.getId(), vo.getUserPwd(), resUserVo.getSalt());
			if( ! resUserVo.getUserPwd().equals(pwd)){
				logger.error(LogUtil.msg("LoginServiceImpl:login", "用户密码不正确", JSON.toJSONString(vo)));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("用户不存在或密码不正确");
				return resultVo;
			}
			break;
		default:
			return resultVo;
		}
		
		String ip = NetHelper.getIpAddr(request);
		logger.info(LogUtil.msg("LoginServiceImpl:login", "用户请求ip=" + ip));
		
		if(StrUtil.isNotNull(resUserVo.getAllowIp())) {
			
			if(! IPUtil.isInIps(ip, resUserVo.getAllowIp())){//不通过则判断
				
				if(StrUtil.isNull(resUserVo.getAllowIpRange()) ||  ! IPUtil.isInIpRange(ip, resUserVo.getAllowIpRange())){
					logger.error(LogUtil.msg("LoginServiceImpl:login", "用户IP受限:" + JSON.toJSONString(vo), ip));
					resultVo.setCode(CodeConstant.FAIL);
					resultVo.setMsg("该账号不能在该电脑登录，请与管理员联系修改ip绑定");
					return resultVo;
				}
				

			}
		} else {
			
			if(StrUtil.isNull(resUserVo.getAllowIpRange()) || ! IPUtil.isInIpRange(ip, resUserVo.getAllowIpRange())){
				logger.error(LogUtil.msg("LoginServiceImpl:login", "用户IP受限:" + JSON.toJSONString(vo), ip));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("该账号不能在该电脑登录，请与管理员联系修改ip绑定");
				return resultVo;
			}
		}
		
		boolean flag = false;
		
		if(resUserVo.getUseRoleList().size() > 1){

			flag = true;
		}
		
		//如果是查询员，判断工作时间
		if (! flag && UserUtil.checkRoleType(resUserVo.getUseRoleList(), CommonConstant.ROLE_OPRTATOR)) {
			
			if(! isWork(resUserVo, request)){
				logger.error(LogUtil.msg("LoginServiceImpl:login", "非工作时间不能登录系统", ip));
				resultVo.setCode(CodeConstant.FAIL);
				resultVo.setMsg("非工作时间不能登录系统");
				return resultVo;
			}
		}
		
		String sid = UserUtil.getSid(resUserVo.getId());
		
		Object object = redisService.getObject(CommonConstant.CACHE_LOGIN_PREFIX + resUserVo.getId());
//		
//		if(object != null){
//			
//			logger.error("LoginServiceImpl:login", "用户已登录", JSON.toJSONString(vo), ip);
//			resultVo.setCode(CodeConstant.FAIL);
//			resultVo.setMsg("用户已登录");
//			return resultVo;
//		}
		UserModel userModel = new UserModel();
		
		if(StrUtil.isNotNull(resUserVo.getRealUserId())){
			
			ResultVo<ResRealUserVo> resultRealUser = realUserService.login(resUserVo.getRealUserId(), resUserVo.getId(), request);

			if(! CodeConstant.SUCCESS.equals(resultRealUser.getCode())){
				logger.error(LogUtil.msg("LoginServiceImpl:login", "登录征信中心失败", JSON.toJSONString(vo), ip));
				resultVo.setCode(resultRealUser.getCode());
				resultVo.setMsg(resultRealUser.getMsg());
				return resultVo;
			}
			
			ResRealUserVo resRealUserVo = resultRealUser.getData();
			userModel.setRealUserAccount(resRealUserVo.getAccount());
			userModel.setRealUserId(resUserVo.getRealUserId());
		}
		
		resultVo.setCode(CodeConstant.SUCCESS);
		
		userModel.setAccount(vo.getAccount());
		userModel.setIp(NetHelper.getIpAddr(request));
		userModel.setSid(sid);
		userModel.setUserId(resUserVo.getId());
		userModel.setRoleList(resUserVo.getUseRoleList());
		userModel.setCookiesKey(RandomUtil.randomStr(10));
		userModel.setAuthType(vo.getAuthType());
		userModel.setUserName(resUserVo.getUserName());
		userModel.setHeadUrl(resUserVo.getHeadUrl());
		userModel.setCertifiedHeadUrl(resUserVo.getCertifiedHeadUrl());
		userModel.setCorpName(resUserVo.getCorpName());
		userModel.setCorpId(resUserVo.getCorpId());
		userModel.setDeptId(resUserVo.getDeptId());
		userModel.setSignName(resUserVo.getSignName());
		userModel.setLimitCount(resUserVo.getLimitCount());
		getUseMenuAndPower(resUserVo.getUseRoleList(), userModel, request);
		
		UserUtil.setUserModel(userModel, redisService);
		executor.execute(new Runnable() {
			
			@Override
			public void run() {
				
				OnlineUserVo onlineUserVo = JSON.parseObject(JSON.toJSONString(userModel), OnlineUserVo.class);
				onlineUserVo.setSessionId(userModel.getSid());
				onlineUserVo.setLastActiveTime(DateUtil.getCurrentFormatMill());
				logger.info(LogUtil.msg("LoginServiceImpl:login", "用户登录成功,将登录用户写入mongondb,视为在线用户:" + JSON.toJSONString(onlineUserVo)));
				onlineUserService.add(onlineUserVo, request);	
				if(object != null){
					
					OnlineUserVo onlineUser = new OnlineUserVo();
					onlineUserVo.setSessionId(JSON.parseObject(JSON.toJSONString(object), UserModel.class).getSid());
					logger.info(LogUtil.msg("LoginServiceImpl:login", "更新上次未退出sid时间:" + JSON.toJSONString(onlineUser)));
					onlineUserService.userOffline(onlineUser, request);	
				}
			}
		});
		resultVo.setData(userModel);
		
		return resultVo;
	}
	
	private ResultVo<UserModel> loginValid(LoginVo vo, HttpServletRequest request){
		
        ResultVo<UserModel> resultVo = new ResultVo<>();
		
		if (vo == null || StrUtil.isNull(vo.getAccount()) || vo.getAuthType() == null) {
			logger.error(LogUtil.msg("LoginServiceImpl:loginValid", "请求参数不正确"));
			resultVo.setCode(ResCode.REQ_PARAM_FAIL);
			resultVo.setMsg("账号不能为空");
			return resultVo;
		}
		
		int authType = vo.getAuthType();
		
		switch (authType) {
		
		case CommonConstant.AUTH_TYPE_FACE:
			DictionaryVo dictionaryVo = new DictionaryVo();
			dictionaryVo.setKeyword("FACE_RATIO");
			ResultVo<ResDictionaryVo> resultDic = dictionaryService.getDicByKeyword(dictionaryVo, request);
			int faceRadio = 75;
			if(CodeConstant.SUCCESS.equals(resultDic.getCode())){
				faceRadio = Integer.parseInt(resultDic.getData().getDicValue());
			}
			
			
			if(vo.getSimilar() == null || vo.getSimilar() < faceRadio ){
				logger.error(LogUtil.msg("LoginServiceImpl:loginValid", "人脸比对值不正确，faceRadio:" + vo.getSimilar()));
				resultVo.setCode(ResCode.REQ_PARAM_FAIL);
				resultVo.setMsg("人脸比对值小于系统设定值");
				return resultVo;
			}
			break;
		case CommonConstant.AUTH_TYPE_SMS:
			if ( StrUtil.isNull(vo.getSms())) {
				logger.error(LogUtil.msg("LoginServiceImpl:loginValid", "短信验证码为空"));
				resultVo.setCode(ResCode.REQ_PARAM_FAIL);
				resultVo.setMsg("请输入短信验证码");
				return resultVo;
			}
			break;
		case CommonConstant.AUTH_TYPE_FINGER:			
			if(vo.getFingerFile() == null){
				logger.error(LogUtil.msg("LoginServiceImpl:login", "指纹文件为空"));
				resultVo.setCode(ResCode.REQ_PARAM_FAIL);
				resultVo.setMsg("请上传指纹图片");
				return resultVo;
			}
			break;
		case CommonConstant.AUTH_TYPE_PWD:			
			if ( StrUtil.isNull(vo.getUserPwd())) {
				logger.error(LogUtil.msg("LoginServiceImpl:loginValid", "用户密码为空"));
				resultVo.setCode(ResCode.REQ_PARAM_FAIL);
				resultVo.setMsg("请输入用户密码");
				return resultVo;
			}
			break;
		default:
			
			logger.error(LogUtil.msg("LoginServiceImpl:loginValid", "认证方式不正确"));
			resultVo.setCode(ResCode.REQ_PARAM_FAIL);
			resultVo.setMsg("认证方式不正确");
			return resultVo;
		}
		
		resultVo.setCode(CodeConstant.SUCCESS);
		
		return resultVo;
	}

	@Override
	public ResultVo<UserModel> logout(BaseVo vo, HttpServletRequest request) {
		
		ResultVo<UserModel> resultVo = new ResultVo<>();
        if(vo == null || StrUtil.isNull(vo.getSid())){
        	
        	logger.error(LogUtil.msg("LoginServiceImpl:logout", "请求参数不正确"));
			resultVo.setCode(ResCode.SUCCESS);
			return resultVo;
        }
		
        UserModel userModel = null;
        
        if(StrUtil.isNotNull(vo.getUserId())){
        	
        	Object object = redisService.getObject(CommonConstant.CACHE_LOGIN_PREFIX + vo.getUserId());	
    		if(object == null){    			
    			logger.error(LogUtil.msg("LoginServiceImpl:logout", "用户已退出登录"));
    			resultVo.setCode(ResCode.SUCCESS);
    			return resultVo;
    		}	
    		
    		userModel = JSON.parseObject(JSON.toJSONString(object), UserModel.class);
        } else {
        	
        	userModel = UserUtil.getUserModel(vo.getSid(), redisService);
		}
        
        String sid = userModel.getSid();
        
        UserUtil.delUserModel(userModel.getSid(), redisService);
        
        ResCCUserVo ccUserVo = UserUtil.getRealUserLogin(userModel.getRealUserAccount(), userModel.getUserId(), redisService);
        if(ccUserVo != null){
        	ReqCCUserVo reqCCUserVo = new ReqCCUserVo();
        	reqCCUserVo.setCookies(ccUserVo.getCookies());
        	try {
				ccUserService.logout(reqCCUserVo, request);
				UserUtil.delRealUser(userModel.getRealUserAccount(), userModel.getUserId(),redisService);
			} catch (Exception e) {
				logger.error(LogUtil.msg("LoginServiceImpl:logout", "退出征信中心异常"), e);
			}
        }
       
        executor.execute(new Runnable() {
			
			@Override
			public void run() {
				
				OnlineUserVo onlineUserVo = new OnlineUserVo();
				onlineUserVo.setSessionId(sid);
				logger.info(LogUtil.msg("LoginServiceImpl:logout", "用户退出更新:" + JSON.toJSONString(onlineUserVo)));
				onlineUserService.userOffline(onlineUserVo, request);				
			}
		});
        
        resultVo.setCode(ResCode.SUCCESS);
        
		return resultVo;
	}

	@Override
	public ResultVo<String> findFaceCertifiedImg(LoginVo vo, HttpServletRequest request, HttpServletResponse response) {

		ResultVo<String> resultVo = new ResultVo<>();
		
		if (vo == null || StrUtil.isNull(vo.getAccount())) {
			logger.error(LogUtil.msg("LoginServiceImpl:findFaceCertifiedImg", "请求参数不正确"));
			resultVo.setCode(ResCode.REQ_PARAM_FAIL);
			resultVo.setMsg("账号不能为空");
			return resultVo;
		}
		
		UserVo userVo = new UserVo();
		userVo.setAccount(vo.getAccount());
		ResUserVo resUserVo = userApi.query(userVo);
		if(resUserVo == null){
			logger.error(LogUtil.msg("LoginServiceImpl:findFaceCertifiedImg", "用户账户不存在", JSON.toJSONString(vo)));
			resultVo.setCode(CodeConstant.DATA_NOT_EXSIT);
			resultVo.setMsg("用户账号不正确");
			return resultVo;
		}
		
		if (StrUtil.isNull(resUserVo.getCertifiedHeadUrl())) {
			logger.error(LogUtil.msg("LoginServiceImpl:findFaceCertifiedImg", "用户账户未上传认证头像", JSON.toJSONString(vo)));
			resultVo.setCode(CodeConstant.FAIL);
			resultVo.setMsg("该用户未上传认证头像");
			return resultVo;
		}
		
		FileVo fileVo = new FileVo();
		fileVo.setId(resUserVo.getCertifiedHeadUrl());
		try {
			resultVo = fileService.getFilePath(fileVo, request);
			if(! CodeConstant.SUCCESS.equals(resultVo.getCode())){
				logger.error(LogUtil.msg("LoginServiceImpl:findFaceCertifiedImg", "获取文件地址失败", JSON.toJSONString(vo)));
				return resultVo;
			}
			
			String fileBase64 = FileUtil.encodeBase64File(resultVo.getData());
			resultVo.setData(fileBase64);
		} catch (Exception e) {			
			logger.error(LogUtil.msg("LoginServiceImpl:findFaceCertifiedImg", "获取文件异常", JSON.toJSONString(vo)), e);
			resultVo.setCode(CodeConstant.EXCEPTION);
			resultVo.setMsg("获取文件异常");
			return resultVo;
		}
		
		resultVo.setCode(CodeConstant.SUCCESS);
		
		return resultVo;
		
	}

	@Override
	public ResultVo<Map<String, Object>> loginConf(LoginVo vo, HttpServletRequest request) {

		DictionaryVo dictionaryVo = new DictionaryVo();
		dictionaryVo.setKeyword("LOGIN_AUTH_TYPE");
		ResultVo<ResDictionaryVo> resultDic = dictionaryService.getDicByKeyword(dictionaryVo, request);
		Map<String, Object> map = new HashMap<>();
		map.put("defaultLoginType", "1");
		if(CodeConstant.SUCCESS.equals(resultDic.getCode())){
			map.put("defaultLoginType", resultDic.getData().getDicValue());
		}
		
		dictionaryVo.setKeyword("FACE_RATIO");
		resultDic = dictionaryService.getDicByKeyword(dictionaryVo, request);
		map.put("faceRadio", "75");
		if(CodeConstant.SUCCESS.equals(resultDic.getCode())){
			map.put("faceRadio", resultDic.getData().getDicValue());
		}
		
		ResultVo<Map<String, Object>> resultVo = new ResultVo<>();
		
		resultVo.setCode(CodeConstant.SUCCESS);
		resultVo.setData(map);
		
		return resultVo;
	}

	private void getUseMenuAndPower(List<ResRoleVo> listRole, UserModel userModel, HttpServletRequest request){
		
		if(listRole == null || listRole.size() == 0){
			return ;
		}
		
		List<String> roleIds = new ArrayList<>();
		
		for (ResRoleVo resRoleVo : listRole) {
			roleIds.add(resRoleVo.getRoleId());
		}
		
		RoleMenuVo roleMenuVo = new RoleMenuVo();
		roleMenuVo.setRoleIds(roleIds);
		
		List<String> useMenuIds = roleMenuApi.listResId(roleMenuVo);
		
		ResultVo<List<ResMenuVo>> resultVo = menuService.getCacheTreeMenu(new MenuVo(), request);
		
		if(! CodeConstant.SUCCESS.equals(resultVo.getCode())){
			return ;
		}
		
		List<ResMenuVo> listMenu = resultVo.getData();
		Map<String, ResMenuVo> map = new HashMap<>();
		for (ResMenuVo resMenuVo : listMenu) {
			
			boolean parentCheck = false;
			
			ResMenuVo temp = new ResMenuVo();
			
			if(useMenuIds.contains(resMenuVo.getId())){
				if(resMenuVo.getResType() == 1){
					resMenuVo.setIsCheck(1);
					parentCheck = true;
				}
				
				if(resMenuVo.getResType() == 2){
					temp.setUri(resMenuVo.getUri());
					temp.setResType(resMenuVo.getResType());
					temp.setName(resMenuVo.getName());
					map.put(resMenuVo.getCode(), temp);
				}
				
			}
			
			List<ResMenuVo> childList = resMenuVo.getChildList();
			
			if(childList != null && childList.size() > 0){
				for (ResMenuVo childMenu : childList) {
					ResMenuVo child = new ResMenuVo();
					if(parentCheck || useMenuIds.contains(childMenu.getId())){
						
						if(childMenu.getResType() == 1){
							childMenu.setIsCheck(1);
							if(! parentCheck){
								resMenuVo.setIsCheck(1);
							}
						}
						if(childMenu.getResType() == 2){
							child.setUri(childMenu.getUri());
							child.setResType(childMenu.getResType());
							child.setName(childMenu.getName());
							map.put(childMenu.getCode(), child);
						}
													
					}
				}
			}
			
		}
		
		userModel.setMenuList(listMenu);
		userModel.setPowerMap(map);
	}
	
	private boolean isWork(ResUserVo resUserVo, HttpServletRequest request){
		
		try {
			logger.info(LogUtil.msg("LoginServiceImpl:isWork", "是否上班时间判断"));
			WorkVo workVo = new WorkVo();
			workVo.setDay(new Date());
			ResWorkVo resWorkVo = workApi.query(workVo);
			if(resWorkVo == null){//数据库还没有初始化，暂时定义为可用工作日
			  logger.info(LogUtil.msg("LoginServiceImpl:isWork", "未初始化该日期，默认为正常上班时间"));
			  return true;	
			}
			if(resWorkVo.getIsWork() == 1){
				DictionaryVo dictionaryVo = new DictionaryVo();
				dictionaryVo.setKeyword("WORK_TIME_BANK");
				ResultVo<ResDictionaryVo> resultDic = dictionaryService.getDicByKeyword(dictionaryVo, request);
				
				if(CodeConstant.SUCCESS.equals(resultDic.getCode())){
					String dicVal = resultDic.getData().getDicValue();
					String dicFid = resultDic.getData().getDicField1();
					int start = Integer.parseInt(dicVal.replace(":", ""));
					int end = Integer.parseInt(dicFid.replace(":", ""));
					int cur = Integer.parseInt(DateUtil.format(new Date(), "HHmm"));
					if(cur >= start && cur <= end){
						logger.info(LogUtil.msg("LoginServiceImpl:isWork", "正常上班时间，可以登录"));
						return true;
					}
				}
			} 
				
			if(resWorkVo.getIsAddWork() == 0){
				logger.info(LogUtil.msg("LoginServiceImpl:isWork", "非工作时间不能登录系统"));
				return false;
			}
			
			AddVo addVo = new AddVo();
			addVo.setDay(new Date());
			addVo.setCorpId(resUserVo.getCorpId());
			addVo.setDeptId(resUserVo.getDeptId());
			addVo.setUserId(resUserVo.getId());
			addVo.setStatus(2);
			ResAddVo resAddVo = addApi.queryUserWork(addVo);
			if(resAddVo == null){
				logger.info(LogUtil.msg("LoginServiceImpl:isWork", "非工作时间不能登录系统"));
				return false;
			}
			
			String onTime = resAddVo.getOnTime();
			String offTime = resAddVo.getOffTime();
			int start = Integer.parseInt(onTime.replace(":", ""));
			int end = Integer.parseInt(offTime.replace(":", ""));
			int cur = Integer.parseInt(DateUtil.format(new Date(), "HHmm"));
			if(cur >= start && cur <= end){
				logger.info(LogUtil.msg("LoginServiceImpl:isWork", "用户加班，可以登录系统"));
				return true;
			}
			
			logger.info(LogUtil.msg("LoginServiceImpl:isWork", "非工作时间不能登录系统"));
			return false;
		} catch (Exception e) {
			logger.info(LogUtil.msg("LoginServiceImpl:isWork", "查询判断工作时间异常，默认可以登录系统"), e);
			return true;
		}
	}
}
